package yja.gzw.app.activity

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import yja.gzw.app.R
import yja.gzw.app.adapter.FabuAdapter
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityItJvjueBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import java.io.File

class ItJvjueActivity :BaseActivity<Mainviewmodel, ActivityItJvjueBinding>(R.layout.activity_it_jvjue), FabuAdapter.OnItemClickListener{


    var file: File?=null
    public var list= mutableListOf<ZaoShuju>(
        ZaoShuju(2,""),

        )
    private lateinit var fabiada: FabuAdapter
    override fun initView() {

        val oids =intent.getStringExtra("oids")
        val type =intent.getStringExtra("type")




            binding.tijiao.setOnClickListener {
                val builder = androidx.appcompat.app.AlertDialog.Builder(this)
                builder.setTitle("提示")
                    .setMessage("确定要取消吗？")
                    .setNegativeButton("取消") { dialog, _ ->
                        dialog.dismiss() // 关闭对话框


                        val neirong=binding.neirong.text.toString()
                        var imagess=""
                        for ((indenx,zaoShuju) in list.withIndex()) {
                            if(indenx==list.size-1){
                                imagess=imagess+zaoShuju.tupian
                            }else{

                                imagess=imagess+zaoShuju.tupian+","
                            }

                        }


                        viewmodel.onitjvjuedingdanwang(UserData.token, mapOf("order_id" to oids!!,"type" to type!!,"refuse_reason" to neirong,"refuse_reason_images" to imagess))





                    }
                    .setPositiveButton("确认") { _, _ ->
                        // 在这里添加确认逻辑

                                val neirong=binding.neirong.text.toString()
                        var imagess=""


                        for ((indenx,zaoShuju) in list.withIndex()) {
                            Log.e("图片路径展示","${indenx}zaoShuju")

                            if(indenx==list.size-1){
                                imagess=imagess+zaoShuju.tupian
                            }else{
                                imagess=imagess+zaoShuju.tupian+","
                            }

                        }



                        viewmodel.onitjvjuedingdanwang(UserData.token, mapOf("order_id" to oids!!,"type" to "1","refuse_reason" to neirong,"refuse_reason_images" to imagess))



                    }
                val dialog = builder.create()
                dialog.show()
            }








        binding.review.layoutManager=
            GridLayoutManager(this, 3)
        fabiada= FabuAdapter(this,list,this)
        binding.review.adapter= fabiada
    }

    override fun dataObservese() {

        viewmodel.onitjvjuedingdan.observe(this, Observer {
            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                finish()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })

        viewmodel.onlusc.observe(this, Observer {
            if(it.code==1){



                list.add(0,ZaoShuju(1,it.data.url))
                Log.e("上传图片的半路径",it.data.url)
                binding.review.layoutManager=
                    GridLayoutManager(this, 3)
                fabiada= FabuAdapter(this,list,this)
                binding.review.adapter= fabiada

                Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show()




            }


        })
    }

    override fun onItemClick(position: Int, uri: ZaoShuju) {
        if(uri.leixing==2){
            openGallery()

        }else{
            Toast.makeText(this, "图片预览中"+"    "+list.size, Toast.LENGTH_SHORT).show()


        }
    }
    private fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, GALLERY_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            data?.data?.let { uri ->
                file = File(getRealPathFromURI(uri))

                val requestFile = file?.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val body = MultipartBody.Part.createFormData("file", file?.name,requestFile!!)
                viewmodel.onluying(body)
            }
        }
    }

    private fun getRealPathFromURI(contentUri: Uri): String {
        val projection = arrayOf(MediaStore.Images.Media.DATA)
        val cursor = contentResolver.query(contentUri, projection, null, null, null)
        val column_index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor?.moveToFirst()
        val result = cursor?.getString(column_index!!)
        cursor?.close()
        return result!!
    }
    companion object {
        private const val GALLERY_REQUEST_CODE = 1001
    }
}